package com.wwdx.controller.rest;

import com.google.common.base.Preconditions;
import com.wwdx.controller.base.BaseController;
import com.wwdx.utils.ConstantHolder;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author liguoliang
 * @date 2017/6/15.
 */
@RestController
@RequestMapping("statistics")
@ApiIgnore
public class RestStatisticsController extends BaseController{

    private final String pattern = ConstantHolder.Constant.LOCAL_DATE_PATTERN;

    /**
     * 页面PV、UV统计
     * @param channel 渠道
     * @param unionid 用户业务ID
     * @param type 1 首页、2 搜索、3 赠一得一、4 会员、5 必听书单列表、6 书单详情、
     *             7 解读书库、8 精品课列表、9 课程详情页、10 小程序首页、11 小程序我的、12 优惠卷
     *             13、小课 14、音频下载列表
     */
    @RequestMapping("/page")
    public void page(String channel, String unionid,Integer type){
        Preconditions.checkArgument(StringUtils.isNotBlank(channel),"channel is not null");
        Preconditions.checkArgument(type != null && type > 0,"type is not null");
        String date = new LocalDate().toString(pattern);
        //PV
        String pvKey = String.format(ConstantHolder.PAGE_PV_LIST,date,channel,type);
        listOperations.leftPush(pvKey, unionid);
    }

    /**
     * 会员页面PV、UV统计
     * @param unionid 用户业务ID
     * @param merchantId 商户ID
     */
    @RequestMapping("/vip_count")
    public void vipCount(String unionid, @RequestParam(value = "merchant_id") String merchantId){
        String date = new LocalDate().toString(pattern);
        //商户分销统计
        if (StringUtils.isNotBlank(merchantId)){
            String merchantPVKey = String.format(ConstantHolder.MERCHANT_VIP_PV_LIST,merchantId,date);
            listOperations.leftPush(merchantPVKey,unionid);
        }
    }

    /**
     * 主题包统计
     * @param themeid 主题包ID
     * @param channel 渠道
     * @param unionid 用户业务ID
     * @param merchantId 商户ID
     */
    @RequestMapping("/readtheme_count")
    public void readthemeCount(Long themeid, String channel, String unionid,@RequestParam(value = "merchant_id") String merchantId){
        Preconditions.checkArgument(StringUtils.isNotBlank(channel),"channel is not null");
        Preconditions.checkArgument(themeid != null && themeid >0,"themeid is null or 0");
        String date = new LocalDate().toString(pattern);
        wwdxThemeService.themeStatistics(themeid);
        //PV
        String pvKey = String.format(ConstantHolder.THEME_DAY_USER_LIST, date,channel,themeid);
        listOperations.leftPush(pvKey, unionid);
        //商户分销统计
        if (StringUtils.isNotBlank(merchantId)){
            String merchantPVKey = String.format(ConstantHolder.MERCHANT_THEME_PV_LIST,merchantId,date,themeid);
            listOperations.leftPush(merchantPVKey,unionid);
        }
    }

    /**
     * 书籍统计
     * @param bookid 书籍ID
     * @param channel 渠道
     * @param unionid 用户业务ID
     */
    @RequestMapping("/book_count")
    public void bookCount(Long bookid,String channel,String unionid){
        Preconditions.checkArgument(StringUtils.isNotBlank(channel),"channel is not null");
        Preconditions.checkArgument(bookid != null && bookid > 0,"bookid is not null or 0");
        String date = new LocalDate().toString(pattern);
        wwdxBookService.bookStatistics(bookid);
        //PV
        String pvKey = String.format(ConstantHolder.BOOK_DAY_USER_LIST,date,channel,bookid);
        listOperations.leftPush(pvKey, unionid);
    }

    /**
     * 书的章节的统计
     * @param chapterid 书籍章节ID
     * @param channel 渠道
     * @param unionid 用户业务ID
     */
    @RequestMapping("/bookchapter_count")
    public void bookchapterCount(Long chapterid,String channel,String unionid){
        Preconditions.checkArgument(StringUtils.isNotBlank(channel),"channel is not null");
        Preconditions.checkArgument(chapterid!=null && chapterid > 0,"chapterid is not null or 0");
        wwdxBookChapterService.bookchapterStatistics(chapterid);
        String date = new LocalDate().toString(pattern);
        //PV
        String pvKey = String.format(ConstantHolder.CHAPTER_DAY_USER_LIST,date,channel,chapterid);
        listOperations.leftPush(pvKey, unionid);
    }

    /**
     * 赠一得一统计
     * @param bookid 书籍ID
     * @param channel 渠道
     * @param unionid 用户业务ID
     */
    @RequestMapping("/giveone")
    public void giveonechannel(Long bookid,String unionid,String channel){
        Preconditions.checkArgument(StringUtils.isNotBlank(channel),"channel is not null");
        Preconditions.checkArgument(bookid!=null && bookid > 0,"bookid is not null or 0");
        Preconditions.checkArgument(StringUtils.isNotBlank(unionid),"unionid is not null");
        String date = new LocalDate().toString(pattern);
        //channel pv
        String channelPVKey = String.format(ConstantHolder.GIVEONE_STATISTICS_CHANNEL_PV_LIST,date,channel);
        //book pv
        String bookPVKey = String.format(ConstantHolder.GIVEONE_STATISTICS_BOOK_PV_LIST,date,bookid);
        //user pv
        String userPVKey = String.format(ConstantHolder.GIVEONE_STATISTICS_USER_PV_LIST, date, unionid);
        listOperations.leftPush(channelPVKey, unionid);
        listOperations.leftPush(bookPVKey, unionid);
        listOperations.leftPush(userPVKey,String.valueOf(bookid));
    }
}